package com.srzk.youthleagueback.service;

import com.srzk.youthleagueback.entity.po.Document;
import com.srzk.youthleagueback.mapper.DocumentMapper;
import com.srzk.youthleagueback.results.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.srzk.youthleagueback.results.CodeMsg.*;

@Service
public class DocumentService {
    @Autowired
    DocumentMapper documentMapper;

    public Result<Document> get(String username,String course) {
        Document document = documentMapper.selectByPrimaryKey(username,course);
        if(document==null) {
            return Result.error(NOT_EXIST);
        } else {
            return Result.success(document);
        }
    }


  /*  public Result<List<Document>>  getByUserName(String username){
        List<Document> documents = documentMapper.selectByUserName(username);
        if(documents==null||documents.isEmpty()){
            return Result.error(NOT_EXIST);
        }else{
            return Result.success(documents);
        }
    }*/

    public Result<List<Document>>  getByCourse(String course){
        List<Document> documents = documentMapper.selectByCourse(course);
        if(documents==null||documents.isEmpty()){
            return Result.error(NOT_EXIST);
        }else{
            return Result.success(documents);
        }
    }

    public Result<Document> update(Document document) {
        int result = documentMapper.insert(document);
        if(result==1){
            return Result.success(document);
        }else {
            return Result.error(UPDATE_FAIL);
        }
    }

   /* public Result<String> delete(String url){
        int result = documentMapper.deleteByURL(url);
        if (result==1){
            return Result.success("删除成功");
        }else{
            return Result.error(DELETE_FAIL);
        }
    }*/
}
